package com.api.controller;

import com.api.annotation.ApiAnnotation;
import com.api.constant.CommonConstant;
import com.api.interceptor.ApiInterceptor;
import com.api.model.TMerchant;
import com.api.model.TOrder;
import com.api.model.TUser;
import com.api.service.MerchantService;
import com.api.service.OrderService;
import com.api.utils.DateTimeUtils;
import com.api.utils.JsonReturnUtils;
import com.api.utils.OrderUtils;
import com.api.utils.RollPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by PC on 2017/7/13.
 */
@RestController
@RequestMapping("api/order")
public class OrderController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private OrderService orderService;

    private static final Double Wallet = 0.0;

    @ApiAnnotation
    @RequestMapping(value = "create")
    public Object create(Integer merchantId, Integer type, Boolean isUse, Double pay) {
        TUser user = ApiInterceptor.getUserByRequest();
        TMerchant merchant = (TMerchant) merchantService.findObjByKey(merchantId);
        if (merchant == null) {
            return JsonReturnUtils.fail("B001");
        }
        TOrder order = new TOrder();
        order.setPay(pay);
        Double wallet = user.getWallet();
        if (isUse && wallet > 0) {
            if (wallet >= pay) {
                wallet = wallet - pay;
                order.setActualPay(Wallet);
                order.setWalletPay(pay);
            } else {
                order.setWalletPay(wallet);
                wallet = pay - wallet;
                order.setActualPay(wallet);
            }
        } else {
            order.setActualPay(pay);
            order.setWalletPay(Wallet);
        }
        order.setUserId(user.getId());
        order.setUserName(user.getName());
        order.setMerchantId(merchant.getId());
        order.setMerchantName(merchant.getName());
        order.setPayType(type);
        order.setStatus(CommonConstant.OrderStatus.NoPay.getValue());
        Double cashBackRate = merchant.getCashBackRate();
        Double cashBackUp = merchant.getCashBackUp();
        if (cashBackRate > 0) {
            Double cashBack = pay * cashBackRate;
            if (cashBackUp > 0 && cashBack > cashBackUp) {
                cashBack = cashBackUp;
            }
            order.setCashBack(cashBack);
        } else {
            order.setCashBack(Wallet);
        }
        order.setCreateTime(DateTimeUtils.getTime());
        order.setUpTime(DateTimeUtils.getTime());
        order.setCashBackStatus(CommonConstant.CashBackStatus.Audit.getValue());
        orderService.insertSelective(order);
        return JsonReturnUtils.success(order);
    }

    @ApiAnnotation
    @RequestMapping(value = "list")
    public Object list(Integer page, Integer pageSize) {
        TUser user = ApiInterceptor.getUserByRequest();
        page = (page == null || page == 0) ? 1 : page;
        pageSize = (pageSize == null || pageSize == 0) ? 5 : pageSize;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", user.getId());
        OrderUtils orderUtils = new OrderUtils();
        orderUtils.pushDesc("create_time");
        RollPage<TOrder> rollPage = orderService.findListPageByParams(params, orderUtils, page, pageSize);
        List<TOrder> orderList = rollPage.getRecordList();
        if (orderList.size() > 0) {
            TMerchant merchant;
            for (TOrder order : orderList) {
                merchant = (TMerchant) merchantService.findObjByKey(order.getMerchantId());
                order.setMerchantName(merchant.getName());
                order.setFace(merchant.getFace());
                params = new HashMap<String, Object>();
                params.put("merchantId", merchant.getId());
                params.put("status", CommonConstant.OrderStatus.Comment.getValue());
                Double score = orderService.selectAverageScoreByParams(params);
                score = score == null ? 0 : score;
                order.setScore(score);
            }
        }
        return JsonReturnUtils.success(orderList);
    }
}
